﻿using UnityEngine;
using System.Collections;
using Configs;

public sealed class StageManager
{
    private static readonly StageManager instance = new StageManager();
    public static StageManager GetInstance()
    {
        return instance;
    }

    private IStage stage = null;

    private bool needRemove = false;
    
    public void Start()
    {
        StageObjectFight stageObjectFight = new StageObjectFight();
        stageObjectFight.Start();

        stage = stageObjectFight;
    }

    public void Update(float deltaTime)
    {
        if (null != stage)
        {
            stage.Update(deltaTime);
        }

        if (needRemove)
        {
            if (null != stage)
            {
                needRemove = false;

                stage.Destroy();
                stage = null;
            }
        }
    }

    public IStage GetCurrentStage()
    {
        return stage;
    }

    public void RemoveCurrentStage()
    {
        needRemove = true;
    }

    /// <summary>
    /// 创建UnitObject
    /// </summary>
    /// <param name="stage"></param>
    /// <param name="confId"></param>
    /// <returns></returns>
    public static T Create<T>(IStage stage, int confId) where T : IUnitObject
    {
        ConfUnitObject confUnitObj = ConfUnitObject.Get(confId);
        if (null == confUnitObj)
        {
            return default(T);
        }

        IUnitObject createNew = null;

        switch (confUnitObj.type)
        {
            case 1: //MonsterObject
                createNew = new MonsterObject(stage, confUnitObj);
                return (T)createNew;
            case 2: //TowerObject
                createNew = new TowerObject(stage, confUnitObj);
                return (T)createNew;
            default:
                return default(T);
        }
    }
}
